import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class LRTextField extends StatelessWidget {
  final String leftText;
  final String? unitText;
  final double? horizontalPadding;
  final double verticalPadding;
  final EdgeInsets? padding;
  final bool showLine;
  final TextStyle? leftStyle;
  final TextStyle? rightStyle;
  final TextStyle? disabledStyle;
  final TextStyle? hintStyle;
  final String? hintText;
  final ValueChanged<String>? onChanged;
  final TextEditingController? controller;
  final TextInputType? keyboardType;
  final List<TextInputFormatter>? inputFormatters;
  final bool requiredField;
  final int? maxLength;
  final bool editable;
  final bool autoFocus;
  final bool obscureText;

  const LRTextField({
    Key? key,
    required this.leftText,
    this.hintText = "请输入",
    this.horizontalPadding,
    this.verticalPadding = 5.0,
    this.padding,
    this.unitText,
    this.showLine = true,
    this.rightStyle = const TextStyle(
      fontSize: 15,
      color: Color(0xff252525),
    ),
    this.disabledStyle = const TextStyle(
      fontSize: 15,
      color: Color(0xff999999),
    ),
    this.hintStyle = const TextStyle(
      fontSize: 14,
      color: Color(0xffbfbfbf),
    ),
    this.onChanged,
    this.controller,
    this.keyboardType,
    this.inputFormatters,
    this.requiredField = false,
    this.maxLength,
    this.leftStyle = const TextStyle(
      fontSize: 15,
      color: Color(0xff252525),
    ),
    this.editable = true,
    this.autoFocus = false,
    this.obscureText = false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: padding ??
          EdgeInsets.only(
              top: verticalPadding,
              left: horizontalPadding ?? 12,
              right: horizontalPadding ?? 12),
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Text(leftText, style: leftStyle),
              if (requiredField)
                Text('*',
                    style: leftStyle ??
                        const TextStyle(
                            fontSize: 15, color: Color(0xffF5222D))),
              Expanded(
                  child: TextField(
                autofocus: autoFocus,
                maxLines: 1,
                maxLength: maxLength,
                style: editable ? rightStyle : disabledStyle,
                textAlign: TextAlign.right,
                keyboardType: keyboardType,
                inputFormatters: inputFormatters,
                enabled: editable,
                obscureText: obscureText,
                decoration: InputDecoration(
                    hintText: hintText,
                    hintStyle: hintStyle,
                    focusedBorder: InputBorder.none,
                    enabledBorder: InputBorder.none,
                    errorBorder: InputBorder.none,
                    disabledBorder: InputBorder.none,
                    counterText: ''),
                onChanged: onChanged,
                controller: controller,
              )),
              if (unitText?.isNotEmpty == true)
                Padding(
                  padding: const EdgeInsets.only(left: 4),
                  child: Text(unitText!, style: rightStyle),
                )
            ],
          ),
          SizedBox(height: verticalPadding),
          if (showLine) const Divider(height: 1)
        ],
      ),
    );
  }
}
